#include "OperatorsMeshCraftManager.h"

//OperatorsMeshCraftManager::OperatorsMeshCraftManager()
//{

//}


#include "GUIFrame/MainWindow.h"
#include "GUIFrame/PropertyWidget.h"
#include "GUIDialog/GUICalculateDialog/BoundaryWidget.h"
#include "GUIDialog/GUIMeshDialog/MeshResetNameDialog.h"
#include "GUIDialog/GUIMeshDialog/MeshAssemblyDialog.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "FITK_Kernel/FITKAppFramework/FITKSignalTransfer.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKMeshGenInterface.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKAbstractMesherDriver.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKAbstractMeshProcessor.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKGeometryMeshSize.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKRegionMeshSize.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKRegionGeometryRefine.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Interface/FITKInterfaceMesh/FITKUnstructuredFluidMeshVTK.h"
#include "FITK_Interface/FITKInterfaceMesh/FITKUnstructuredMeshVTK.h"
#include "FITK_Interface/FITKInterfaceFlowOF/FITKOFPhysicsData.h"
#include "FITK_Interface/FITKInterfaceFlowOF/FITKOFBoundary.h"
#include "FITK_Interface/FITKInterfaceFlowOF/FITKOFGeometryData.h"

#include "OperatorsInterface/GraphEventOperator.h"
#include "OperatorsInterface/TreeEventOperator.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include "FITK_Interface/FITKInterfaceFlowOF/FITKFlowPhysicsHandlerFactory.h"
#include <QFile>
#include <QFileInfo>
#include <QDebug>

#include <QDir>


namespace ModelOper
{
    bool OperatorsMeshCraftManager::execGUI()
    {
        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (graphOper == nullptr)return false;
        // 获取模型树控制器
        auto treeOper = Core::FITKOperatorRepo::getInstance()->getOperatorT<EventOper::TreeEventOperator>("ModelTreeEvent");
        if (treeOper == nullptr) return false;

        // 获取属性栏
        GUI::MainWindow* mainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWindow == nullptr)return false;
        GUI::PropertyWidget* propertyWidget = mainWindow->getPropertyWidget();
        if (propertyWidget == nullptr)return false;

        if (_emitter == nullptr)return false;
        QString actionName = _emitter->objectName();
        if (actionName == "UpDie") {
            // 处理 UpDie
            return processAction(actionName);
        } else if (actionName == "LowDie") {
            // 处理 LowDie
            return processAction(actionName);
        } else if (actionName == "Sheet") {
            // 处理 LowDie
            return processAction(actionName);
        } else if (actionName == "MidDie") {
            // 处理 MidDie
            return processAction(actionName);
        } else if (actionName == "Inner") {
            // 处理 Inner
            return processAction(actionName);
        } else if (actionName == "Outer") {
            // 处理 Outer
            return processAction(actionName);
        } else if (actionName == "Punch") {
            // 处理 Punch
            return processAction(actionName);
        } else if (actionName == "Die") {
            // 处理 Die
            return processAction(actionName);
        } else if (actionName == "BH") {
            // 处理 BH
            return processAction(actionName);
        } else if (actionName == "BottomSheet") {
            // 处理 BottomSheet
            return processAction(actionName);
        } else if (actionName == "TopSheet") {
            // 处理 TopSheet
            return processAction(actionName);
        } else if(actionName == "MasterModel"){
            // MasterModel Steeple FixedPlate
            return processAction(actionName);
        } else if(actionName == "Steeple"){
            // MasterModel Steeple FixedPlate
            return processAction(actionName);
        } else if(actionName == "FixedPlate"){
            // MasterModel Steeple FixedPlate
            return processAction(actionName);
        } else {
            emit FITKAPP->getSignalTransfer()->outputMessageSig(3, tr("Error : Unsupported action name !!!"));
            return false;
        }

    }

    bool OperatorsMeshCraftManager::execProfession()
    {
        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (graphOper == nullptr)return false;
        // 获取模型树控制器
        auto treeOper = Core::FITKOperatorRepo::getInstance()->getOperatorT<EventOper::TreeEventOperator>("ModelTreeEvent");
        if (treeOper == nullptr) return false;

        //刷新
        treeOper->updateTree();
        graphOper->reRender();
        return true;
    }
    bool OperatorsMeshCraftManager::processAction(const QString& actionName) {
        auto arg = _operArgs.value("objID");

        Interface::FITKUnstructuredMeshVTK* meshDataFASTSIM = FITKAPP->getGlobalData()->getMeshDataFASTSIM< Interface::FITKUnstructuredMeshVTK>();
        if (meshDataFASTSIM == nullptr) return false;

        Interface::FITKMeshVTKManagerFASTSIM* manager = meshDataFASTSIM->getBoundaryMeshManager();
        for (int i = 0; i < manager->getDataCount(); i++) {
            Interface::FITKUnstructuredMeshVTK* boundMesh = manager->getDataByIndex(i);
            if (boundMesh->getDataObjectID() == arg) {
                boundMesh->setDataObjectName(actionName);
                return true;
            }
        }
        emit FITKAPP->getSignalTransfer()->outputMessageSig(3, tr("Warning : An error occurred during renaming !!!"));
        return false;
    }
}
